<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 线下课程管理Controller
 */
class Offline_course extends MY_Controller {
  public $left_menu_min;

  function __construct(){
  parent::__construct();

      $parent_id = 3;
      //获取school二级菜单
      $this->load->model('School_manage_menu_model');
      $menu_list = $this->School_manage_menu_model->get_menu_list($this->is_manage,$parent_id);
      // print_r($menu_list);exit;
      $this->smarty->assign("menu_list", $menu_list);

      $this->left_menu_min = array("menu" => "课程管理", "curr" => '/course_banke', "list" => $menu_list);
      $this->smarty->assign("left_menu_min", $this->left_menu_min);
      $this->smarty->assign("menu", "course");

  }
  

  /**
   * 线下课程list
   * @param  string $value [description]
   * @return [type]        [description]
   */
  public function index($value='')
  {
    $this->load->model('Courseo2o_model');

    // print_r($this->user_data['school_id']);exit;
    $course_list = $this->Courseo2o_model->get_offline_course($this->user_data['school_id']);

    $this->smarty->assign("course_list", $course_list);
    
		$this->smarty->display("offline/course_list.html");
  }

  /**
   * 添加线下课程
   * @param string $value [description]
   */
  public function course_add($value='')
  {
    $this->load->model('Courseo2o_model');
    //获取课程分类
    $cate_list = $this->Courseo2o_model->get_offline_cate($this->user_data['school_id']);
    $this->smarty->assign("cate_list", $cate_list);
    $this->smarty->assign('cate_id','');
    $this->smarty->assign('unit','');
    $post_data = $this->input->post();
		if($post_data){
      if(empty($post_data['title'])){
        $this->smarty->assign("title_err", "课程名称不能为空");
        $has_error = 1;
      }else{
				$this->smarty->assign("title", $post_data['title']);
			}
			if(empty($post_data['cate_id'])){
        $this->smarty->assign("cate_err", "请选择分类");
        $has_error = 1;
      }else{
				$this->smarty->assign("cate_id", $post_data['cate_id']);
			}
      if(empty($post_data['unit'])){
        $this->smarty->assign("unit_err", "请选择单位");
        $has_error = 1;
      }else{
				$this->smarty->assign("unit", $post_data['unit']);
			}
      if(empty($post_data['class_time'])){
        $this->smarty->assign("class_time_err", "请填写总课时");
        $has_error = 1;
      }else{
				$this->smarty->assign("class_time", $post_data['class_time']);
			}
      if(empty($post_data['one_class'])){
        $this->smarty->assign("one_class_err", "请填写每课时");
        $has_error = 1;
      }else{
				$this->smarty->assign("one_class", $post_data['one_class']);
			}
   //    if(empty($post_data['price'])){
   //      $this->smarty->assign("price_err", "请填写价格");
   //      $has_error = 1;
   //    }else{
			// 	$this->smarty->assign("price", $post_data['price']);
			// }
      if($has_error == 0){
        //添加课程
        $result = $this->Courseo2o_model->offline_course_add($post_data,$this->user_data['school_id']);
  			header("Location:/offline_course");exit;
      }

		}

    
		$this->smarty->display("offline/course_add.html");
  }

  /**
   * 删除课程科目
   * 
   * @param  [type] $id [description]
   * @return [type]     [description]
   */
  function course_del($id){
    $this->load->model('Courseo2o_model');
    $course = $this->Courseo2o_model->get_offline_course_by_id($id);
    if(empty($course)) {
      header("Location:/offline_course");exit;
    }
    if($course['school_id'] != $this->user_data['school_id']){
      header("Location:/offline_course");exit;
    }
    //删除
    $this->Courseo2o_model->delete_offlice_course($id);
  }

  function course_edit($id){
    $this->load->model('Courseo2o_model');
    $post_data = $this->input->post();
    if($post_data){
      if(empty($post_data['title'])){
        $this->smarty->assign("title_err", "课程名称不能为空");
        $has_error = 1;
      }else{
        $this->smarty->assign("title", $post_data['title']);
      }
      if(empty($post_data['cate_id'])){
        $this->smarty->assign("cate_err", "请选择分类");
        $has_error = 1;
      }else{
        $this->smarty->assign("cate_id", $post_data['cate_id']);
      }
      if(empty($post_data['unit'])){
        $this->smarty->assign("unit_err", "请选择单位");
        $has_error = 1;
      }else{
        $this->smarty->assign("unit", $post_data['unit']);
      }
      if(empty($post_data['class_time'])){
        $this->smarty->assign("class_time_err", "请填写总课时");
        $has_error = 1;
      }else{
        $this->smarty->assign("class_time", $post_data['class_time']);
      }
      if(empty($post_data['one_class'])){
        $this->smarty->assign("one_class_err", "请填写每课时");
        $has_error = 1;
      }else{
        $this->smarty->assign("one_class", $post_data['one_class']);
      }
      if(empty($post_data['price'])){
        $this->smarty->assign("price_err", "请填写价格");
        $has_error = 1;
      }else{
        $this->smarty->assign("price", $post_data['price']);
      }
      //添加课程
      $result = $this->Courseo2o_model->offline_course_edit($post_data);
      header("Location:/offline_course");exit;
    }

    $course = $this->Courseo2o_model->get_offline_course_by_id($id);
    if(empty($course)) {
      header("Location:/offline_course");exit;
    }
    if($course['school_id'] != $this->user_data['school_id']){
      header("Location:/offline_course");exit;
    }

    //获取课程分类
    $cate_list = $this->Courseo2o_model->get_offline_cate($this->user_data['school_id']);
    $this->smarty->assign("cate_list", $cate_list);


    $this->smarty->assign("id", $id);
    $this->smarty->assign("title", $course['title']);
    $this->smarty->assign("cate_id", $course['cate_id']);
    $this->smarty->assign("unit", $course['unit']);
    $this->smarty->assign("class_time", $course['class_time']);
    $this->smarty->assign("one_class", $course['one_class']);
    $this->smarty->assign("price", $course['price']);

    $this->smarty->display("offline/course_add.html");
  }

  /**
   * 线下课程分类
   * @param  string $value [description]
   * @return [type]        [description]
   */
  public function cate($value='')
  {
    $this->load->model('Courseo2o_model');
    $cate_list = $this->Courseo2o_model->get_offline_cate($this->user_data['school_id']);

    $this->smarty->assign("cate_list", $cate_list);
    
		$this->smarty->display("offline/course_cate_list.html");
  }

  /**
   * 添加线下课程分类
   * @param string $value [description]
   */
  public function cate_add($value='')
  {
    $post_data = $this->input->post();
		if($post_data){
      if(empty($post_data['name'])){
        $this->smarty->assign("name_err", "分类名称不能为空");
        $has_error = 1;
      }else{
				$this->smarty->assign("name", $post_data['name']);
			}
			if(empty($post_data['sort'])){
        $this->smarty->assign("sort_err", "排序不能为空");
        $has_error = 1;
      }else{
				$this->smarty->assign("sort", $post_data['sort']);
			}
      //添加分类
      $this->load->model('Courseo2o_model');
      $result = $this->Courseo2o_model->offline_cate_add($post_data['name'],$post_data['sort'],$this->user_data['school_id']);
				header("Location:/offline_course/cate");exit;
		}

    
		$this->smarty->display("offline/course_cate_add.html");
  }

  function cate_edit($id){
    $this->load->model('Courseo2o_model');
    $post_data = $this->input->post();
    if($post_data){
      if(empty($post_data['name'])){
        $this->smarty->assign("name_err", "分类名称不能为空");
        $has_error = 1;
      }else{
        $this->smarty->assign("name", $post_data['name']);
      }
      if(empty($post_data['sort'])){
        $this->smarty->assign("sort_err", "排序不能为空");
        $has_error = 1;
      }else{
        $this->smarty->assign("sort", $post_data['sort']);
      }
      //添加分类
      $this->load->model('Courseo2o_model');
      $result = $this->Courseo2o_model->offline_cate_edit($post_data['name'],$post_data['sort'],$post_data['id']);
        header("Location:/offline_course/cate");exit;
    }

    $cate = $this->Courseo2o_model->get_offline_cate_by_id($id);

    if(empty($cate)) {
      header("Location:/offline_course/cate");exit;
    }
    if($cate['school_id'] != $this->user_data['school_id']){
      header("Location:/offline_course/cate");exit;
    }
    $this->smarty->assign("name", $cate['name']);
    $this->smarty->assign("sort", $cate['sort']);
    $this->smarty->assign("id", $cate['id']);
    $this->smarty->display("offline/course_cate_add.html");

  }

  function cate_del($id){
    $this->load->model('Courseo2o_model');
    $cate = $this->Courseo2o_model->get_offline_cate_by_id($id);
    if(empty($cate)) {
      header("Location:/offline_course/cate");exit;
    }
    if($cate['school_id'] != $this->user_data['school_id']){
      header("Location:/offline_course/cate");exit;
    }

    //删除
    $this->Courseo2o_model->delete_offlice_cate($id);
    header("Location:/offline_course/cate");exit;

  }
}
